﻿
using Entity;
using log4net;
using Newtonsoft.Json;
using SysFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Tools;
using static System.Net.Mime.MediaTypeNames;

namespace BLL
{
    public class AutoTaskBLL
    {
        SystemManagement sysBLL;
        public ILog log = LogManager.GetLogger(typeof(SystemManagement));
        public AutoTaskBLL()
        {
            sysBLL = new SystemManagement();
        }
        /// <summary>
        /// 人员同步执行方法(自动任务)
        /// </summary>
        public void auto_User()
        {
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("人员信息同步同步开始执行");
                string baseCode = sysBLL.getDefaultBaseCode();
                UserManagement userBLL = new UserManagement();
                rsl = userBLL.SyncUserData("admin", baseCode);
                //rsl = userBLL.SyncUserData(userCode, erpCode);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            log.Info(string.Format("人员组织同步任务执行完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }

        /// <summary>
        /// 客户同步执行方法(自动任务)
        /// </summary>
        public void auto_Customer()
        {
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("客户信息同步开始执行");
                string baseCode = sysBLL.getDefaultBaseCode();
                CustomBLL cuBLL = new CustomBLL();
                rsl = cuBLL.SyncCustom("admin", baseCode, "Cu", true);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            log.Info(string.Format("客户信息任务执行完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }
        /// <summary>
        /// 供应商同步执行方法(自动任务)
        /// </summary>
        public void auto_Vendor()
        {
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("供应商信息同步开始执行");
                string baseCode = sysBLL.getDefaultBaseCode();
                CustomBLL cuBLL = new CustomBLL();
                rsl = cuBLL.SyncCustom("admin", baseCode, "Sp", true);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            log.Info(string.Format("供应商信息任务执行完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }

        /// <summary>
        /// 存货数据同步
        /// </summary>
        public void sync_Inventory()
        {
            ReturnResult rsl = new ReturnResult();
            bool ischanage = false;
            try
            {
                log.Info("存货档案信息同步开始执行");
                string date = this.GetSysFile("SyncProduct");
                if (string.IsNullOrWhiteSpace(date) || DateTime.Parse(date) < DateTime.Now)
                {
                    string baseCode=sysBLL.getDefaultBaseCode();
                    InventoryBLL  venBLL = new InventoryBLL();
                    rsl = venBLL.SyncInventory("admin", baseCode,true,true);
                    ischanage = true;
                    log.Info(string.Format("存货档案同步任务执行完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
                }
                else
                {
                    log.Info("存货档案同步不执行，原因未到更新同步时间");
                }
            }
            catch (Exception ex)
            {
                log.Error("存货档案异常:"+ ex.Message);
                rsl.Message = ex.Message;
            }
            #region 记录下一次更新时间
            if (ischanage)
            {
                string filePath = string.Empty;
                string ExcelPathFolder = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "sysLog";
                filePath = System.IO.Path.Combine(ExcelPathFolder, "SyncProduct.txt");
                FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                string nDate = string.Format("{0} 01:00:00", DateTime.Now.AddDays(1).ToString("yyyy-MM-dd"));
                sr.Write(nDate);//开始写入值
                sr.Close();
                fs.Close();
            }
            #endregion
        }
      
        /// <summary>
        /// 存货大类数据同步
        /// </summary>
        public void sync_InventoryClass()
        {
            InventoryBLL venBLL = new InventoryBLL();
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("存货大类信息同步开始执行");
                string baseCode = sysBLL.getDefaultBaseCode();
                rsl = venBLL.SyncInventoryClass("admin", baseCode);
            }
            catch (Exception ex)
            {
             rsl.Message=ex.Message;
            }
            log.Info(string.Format("存货大类同步完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }
        /// <summary>
        /// 版辊数据同步方法
        /// </summary>
        public void sync_BanGunData()
        {
            InventoryBLL venBLL = new InventoryBLL();
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("版辊数据信息同步开始执行");
                string baseCode = sysBLL.getDefaultBaseCode();
                rsl = venBLL.SyncBanGunData( baseCode);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            log.Info(string.Format("版辊数据同步完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }
        /// <summary>
        /// 版辊采购入库信息同步获取
        /// </summary>
        public void sync_BanGunIn()
        {
            InventoryBLL venBLL = new InventoryBLL();
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("版辊采购入库信息同步开始执行");
                string baseCode = sysBLL.getDefaultBaseCode();
                rsl = venBLL.SyncBanGunInData(baseCode);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            log.Info(string.Format("版辊采购入库同步完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }
        
        /// <summary>
        /// 仓库同步
        /// </summary>
        public void auto_WareHouse()
        {
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("仓库信息同步开始执行");
                WareHouseBLL wareBLL = new WareHouseBLL();
                string baseCode = sysBLL.getDefaultBaseCode();
                rsl = wareBLL.SyncWareHouse("admin", baseCode,true);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            log.Info(string.Format("仓库信息同步完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }
        public void auto_Dept()
        {
            ReturnResult rsl = new ReturnResult();
            try
            {
                log.Info("部门同步开始执行");
                UserManagement userBLL = new UserManagement();
                string baseCode = sysBLL.getDefaultBaseCode();
                rsl = userBLL.SyncDept("admin", baseCode);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            log.Info(string.Format("部门同步任务执行完成，执行结果：{0}", JsonConvert.SerializeObject(rsl)));
        }
        private string GetSysFile(string filename)
        {
            string filePath = string.Empty;
            string ExcelPathFolder = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "sysLog";
            if (!Directory.Exists(ExcelPathFolder))  //如果Folder不存在，则创建
            {
                Directory.CreateDirectory(ExcelPathFolder);
            }
            filePath = System.IO.Path.Combine(ExcelPathFolder, filename + ".txt");
            if (!System.IO.File.Exists(filePath))
            {
                FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件 

                fs1.Close();
            }
            StreamReader sr = new StreamReader(filePath, Encoding.Default);
            string content;
            content = sr.ReadToEnd();
            sr.Close();
            return content;
        }
       
    }
}
